# coding=utf-8
# !/usr/bin/python
# base on UndCover
# modified by eaudouce
import sys, base64, requests, urllib
from Crypto.Cipher import AES
sys.path.append('..')
from base.spider import Spider

class Spider(Spider):  # 元类 默认的元类 type
    site_name = 'xk_厂长'
    pub_url = 'https://www.cz01.vip/'
    #pub_url = 'https://www.czzy.site/' #备用发布地址
    site_url = ''

    cookies = ''
    headers = {
        'Accept-Encoding': 'gzip, deflate',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36'
    }

    config = {
        'player': {},
        'filter': {}
    }

    def getSiteUrl(self):
        rsp = self.fetch(self.pub_url, headers=self.headers)
        root = self.html(self.cleanText(rsp.text))
        url = root.xpath('//h2/a/@href')[0].strip('/')
        self.site_url = url

    def getName(self):
        return self.site_name

    def init(self, extend=''):
        #初始化不会执行
        print('============{0}============'.format(extend))
        pass

    def homeContent(self, filter):
        result = {}
        cateManual = {
            '豆瓣电影Top250': 'dbtop250',
            '热映中': 'benyueremen',
            '国剧': 'gcj',
            '美剧': 'meijutt',
            '日剧': 'riju',
            '韩剧': 'hanjutv',
            '动漫': 'dm'
        }
        classes = []
        for k in cateManual:
            classes.append({
                'type_name': k,
                'type_id': cateManual[k]
            })
        result['class'] = classes
        return result

    def homeVideoContent(self):
        if '' == self.site_url:
            self.getSiteUrl()

        rsp = self.fetch(self.site_url, headers=self.headers)
        root = self.html(self.cleanText(rsp.text))
        aList = root.xpath("//div[@class='mi_btcon']//ul/li")
        videos = []
        for a in aList:
            name = a.xpath('./a/img/@alt')[0]
            pic = a.xpath('./a/img/@data-original')[0]
            mark = ' '.join(a.xpath("./div[@class='hdinfo']/span/text()")).strip()
            sid = a.xpath("./a/@href")[0]
            sid = self.regStr(sid, "/movie/(\\S+).html")
            videos.append({
                'vod_id': sid,
                'vod_name': name,
                'vod_pic': pic,
                'vod_remarks': mark
            })
        result = {
            'list': videos
        }
        return result

    def categoryContent(self, tid, pg, filter, extend):
        if '' == self.site_url:
            self.getSiteUrl()

        result = {}
        limit = 25
        url = '{2}/{0}/page/{1}'.format(tid, pg, self.site_url)
        rsp = self.fetch(url, headers=self.headers)
        root = self.html(self.cleanText(rsp.text))
        aList = root.xpath("//div[contains(@class,'mi_cont')]//ul/li")
        videos = []
        for a in aList:
            name = a.xpath('./a/img/@alt')[0]
            pic = a.xpath('./a/img/@data-original')[0]
            mark = ' '.join(a.xpath("./div[@class='hdinfo']/span/text()")).strip()
            sid = a.xpath("./a/@href")[0]
            sid = self.regStr(sid, "/movie/(\\S+).html")
            videos.append({
                'vod_id': sid,
                'vod_name': name,
                'vod_pic': pic,
                'vod_remarks': mark
            })

        if len(videos) < limit:
            pagecount = int(pg)
            total = (int(pg) - 1)*limit + len(videos)
        else:
            pagecount = int(pg) + 1
            total = int(pg)*limit + 1

        result['list'] = videos
        result['page'] = pg
        result['pagecount'] = pagecount
        result['limit'] = limit
        result['total'] = total
        return result

    def detailContent(self, array):
        if '' == self.site_url:
            self.getSiteUrl()

        tid = array[0]
        url = '{1}/movie/{0}.html'.format(tid, self.site_url)
        rsp = self.fetch(url, headers=self.headers)
        root = self.html(self.cleanText(rsp.text))
        node = root.xpath("//div[@class='dyxingq']")[0]
        pic = node.xpath(".//div[@class='dyimg fl']/img/@src")[0]
        title = node.xpath('.//h1/text()')[0]
        detail = root.xpath(".//div[@class='yp_context']//p/text()")[0]
        vod = {
            'vod_id': tid,
            'vod_name': title,
            'vod_pic': pic,
            'type_name': '',
            'vod_year': '',
            'vod_area': '',
            'vod_remarks': '',
            'vod_actor': '',
            'vod_director': '',
            'vod_content': detail
        }
        infoArray = node.xpath(".//ul[@class='moviedteail_list']/li")
        for info in infoArray:
            content = info.xpath('string(.)')
            if content.startswith('类型'):
                tpyen = ''
                for inf in info:
                    tn = inf.text
                    tpyen = tpyen +'/'+'{0}'.format(tn)
                    vod['type_name'] = tpyen.strip('/')
            if content.startswith('地区'):
                tpyeare = ''
                for inf in info:
                    tn = inf.text
                    tpyeare = tpyeare +'/'+'{0}'.format(tn)
                    vod['vod_area'] = tpyeare.strip('/')
            if content.startswith('豆瓣'):
                vod['vod_remarks'] = content
            if content.startswith('主演'):
                tpyeact = ''
                for inf in info:
                    tn = inf.text
                    tpyeact = tpyeact +'/'+'{0}'.format(tn)
                    vod['vod_actor'] = tpyeact.strip('/')
            if content.startswith('导演'):
                tpyedire = ''
                for inf in info:
                    tn = inf.text
                    tpyedire  = tpyedire  +'/'+'{0}'.format(tn)
                    vod['vod_director'] = tpyedire .strip('/')
        vod_play_from = '$$$'
        playFrom = [self.site_name]
        vod_play_from = vod_play_from.join(playFrom)
        vod_play_url = '$$$'
        playList = []
        vodList = root.xpath("//div[@class='paly_list_btn']")
        for vl in vodList:
            vodItems = []
            aList = vl.xpath('./a')
            for tA in aList:
                href = tA.xpath('./@href')[0]
                name = tA.xpath('./text()')[0]
                tId = self.regStr(href, '/v_play/(\\S+).html')
                vodItems.append(name + '$' + tId)
            joinStr = '#'
            joinStr = joinStr.join(vodItems)
            playList.append(joinStr)
        vod_play_url = vod_play_url.join(playList)

        vod['vod_play_from'] = vod_play_from
        vod['vod_play_url'] = vod_play_url
        result = {
            'list': [
                vod
            ]
        }
        return result

    def searchContent(self, key, quick):
        if '' == self.site_url:
            self.getSiteUrl()

        headers = self.headers
        headers['Referer'] = self.site_url
        rsp_session = requests.Session()

        url = '{1}/xssearrbch?q={0}'.format(urllib.parse.quote(key), self.site_url)
        rsp = rsp_session.get(url, headers=headers)
        root = self.html(self.cleanText(rsp.text))

        if root.xpath('//title/text()')[0].strip() == '人机验证':
            result = eval(root.xpath("//div[@class='erphp-search-captcha']/form/text()")[0].strip(' ='))
            headers['Referer'] = url
            rsp = rsp_session.post(url, data={'result':result}, headers=headers)
            if rsp.text.find('<script>location.reload();</script>') > -1:
                rsp = rsp_session.get(url, headers=headers)
                root = self.html(self.cleanText(rsp.text))
            else:
                return {'list': []}

        vodList = root.xpath("//div[contains(@class,'mi_ne_kd')]/ul/li/a")
        videos = []
        for vod in vodList:
            name = vod.xpath('./img/@alt')[0]
            pic = vod.xpath('./img/@data-original')[0]
            href = vod.xpath('./@href')[0]
            tid = self.regStr(href, 'movie/(\\S+).html')
            res = vod.xpath('./div[@class="jidi"]/span/text()')
            if len(res) == 0:
                remark = '全1集'
            else:
                remark = vod.xpath('./div[@class="jidi"]/span/text()')[0]
            videos.append({
                'vod_id': tid,
                'vod_name': name,
                'vod_pic': pic,
                'vod_remarks': remark
            })
        return {'list': videos}

    def parseCBC(self, enc, key, iv):
        keyBytes = key.encode('utf-8')
        ivBytes = iv.encode('utf-8')
        cipher = AES.new(keyBytes, AES.MODE_CBC, ivBytes)
        msg = cipher.decrypt(enc)
        paddingLen = msg[len(msg) - 1]
        return msg[0:-paddingLen]

    def playerContent(self, flag, id, vipFlags):
        if '' == self.site_url:
            self.getSiteUrl()

        url = '{1}/v_play/{0}.html'.format(id, self.site_url)
        pat = '\\"([^\\"]+)\\";var [\\d\\w]+=function dncry.*md5.enc.Utf8.parse\\(\\"([\\d\\w]+)\\".*md5.enc.Utf8.parse\\(([\\d]+)\\)'
        rsp = self.fetch(url, headers=self.headers)
        html = rsp.text
        content = self.regStr(html, pat)
        if content == '':
            return {}
        key = self.regStr(html, pat, 2)
        iv = self.regStr(html, pat, 3)
        decontent = self.parseCBC(base64.b64decode(content), key, iv).decode()
        urlPat = 'video: \\{url: \\\"([^\\\"]+)\\\"'
        vttPat = 'subtitle: \\{url:\\\"([^\\\"]+\\.vtt)\\\"'
        str3 = self.regStr(decontent, urlPat)
        str4 = self.regStr(decontent, vttPat)
        self.loadVtt(str3)
        result = {
            'parse': '0',
            'playUrl': '',
            'url': str3,
            'header': ''
        }
        if len(str4) > 0:
            result['subf'] = '/vtt/utf-8'
            # result['subt'] = Proxy.localProxyUrl() + "?do=czspp&url=" + URLEncoder.encode(str4)
            result['subt'] = ''
        return result

    def loadVtt(self, url):
        pass

    def isVideoFormat(self, url):
        pass

    def manualVideoCheck(self):
        pass

    def localProxy(self, param):
        action = {}
        return [200, 'video/MP2T', action, '']
